package com.dongdongshop.controller;

import com.dongdongshop.common.AjaxResult;
import com.dongdongshop.common.PageResult;
import com.dongdongshop.common.ResultEnum;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.service.TbSpecificationService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("specification")
@Controller
public class SpecificationController {
    Logger logger = LoggerFactory.getLogger(SpecificationController.class);
    @DubboReference
    private TbSpecificationService tbSpecificationService;

    /*规格管理页面跳转*/
    @RequestMapping("runSpecification")
    public String runSeller(){
        return "admin/specification";
    }
    /*规格管理页面展示*/
    @RequestMapping("selectInfoLimitServer")
    @ResponseBody
    public PageResult<TbSpecification> selectInfoLimitServer(TbSpecification specification, @RequestParam(value = "pageNumber",defaultValue = "1") Integer pageNumber,
                                                             @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize) {
        AjaxResult pageBean = tbSpecificationService.selectInfoLimitServer(specification,pageNumber, pageSize);
        return (PageResult<TbSpecification>)pageBean.getData();
    }

    /*规格添加*/
    @RequestMapping("addSpecification")
    @ResponseBody
    public AjaxResult addSpecification(TbSpecification specification,@RequestParam("optionNames[]") String[] optionNames,@RequestParam("orders[]") Integer[] orders){

        if (specification.getSpecName()==null||"".equals(specification.getSpecName())){
            return AjaxResult.R(ResultEnum.ERROR);
        }
        try {
            int i = tbSpecificationService.addSpecificationAndOptions(specification,optionNames,orders);
            if (i> 0){
                return AjaxResult.R(ResultEnum.SUCCESS);
            }
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (Exception e){
            logger.error("添加失败!错误详情:{}",e);
            return AjaxResult.R(ResultEnum.ERROR);
        }
    }
    /*修改页面回显*/
    @RequestMapping("/runUpdateSpecification/{id}")
    public String runUpdateSpecification(Model model, @PathVariable("id") Long id){
        /*更具id查找tb_specification*/
        TbSpecification specification = tbSpecificationService.selectByIdSpecification(id);
        /*更具id查找tb_specification_option*/
        List<TbSpecificationOption> list = tbSpecificationService.selectBySpecIdSpecificationOption(id);
        model.addAttribute("specification",specification);
        model.addAttribute("list",list);
        return "/admin/updatespecification";
    }
    /*执行修改*/
    @RequestMapping("updateSpecification")
    @ResponseBody
    public AjaxResult updateSpecification(TbSpecification specification,String[] optionName,Integer[] orders){
        if (specification.getSpecName()==null||"".equals(specification.getSpecName())){
            return AjaxResult.R(ResultEnum.ERROR);
        }
        try {
            int i = tbSpecificationService.updateSpecification(specification,optionName,orders);
            if (i> 0){
                return AjaxResult.R(ResultEnum.SUCCESS);
            }
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (Exception e){
            logger.error("添加失败!错误详情:{}",e);
            return AjaxResult.R(ResultEnum.ERROR);
        }
    }
    /*删除*/
    @RequestMapping("/deleteSpecification/{id}")
    @ResponseBody
    public AjaxResult deleteSpecification(@PathVariable("id") Long id){
        if (id==null){
            return AjaxResult.R(ResultEnum.ERROR);
        }
        try {
            int i = tbSpecificationService.deleteSpecification(id);
            if (i> 0){
                return AjaxResult.R(ResultEnum.SUCCESS);
            }
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (Exception e){
            logger.error("添加失败!错误详情:{}",e);
            return AjaxResult.R(ResultEnum.ERROR);
        }

    }
}
